<template>
  <div class="login-page">
    <!-- 标题 -->
    <van-nav-bar class="registerTitle" title="面试宝典账号注册" />
    <!-- 表单 -->
    <van-form @submit="onSubmit" class="loginForm">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >注册</van-button
        >
      </div>
    </van-form>
    <!-- 注册按钮 -->
    <router-link to="login" class="link">已有帐号？去登录</router-link>
  </div>
</template>

<script>
import { registerApi } from '@/api/user'
export default {
  name: 'register-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      try {
        await registerApi(values)
        this.$toast('注册成功')
        this.username = ''
        this.password = ''
        this.$router.push('/login')
      } catch (err) {
        if (err.response) {
          this.$toast(err.response.data.message)
        } else {
          this.$toast('注册失败')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .registerTitle .van-nav-bar__title {
  color: rgb(129, 137, 250);
  font-weight: 700; /* 设置标题文字大小 */
}
.link {
  float: right;
  margin-right: 10px;
  color: rgb(25, 137, 250);
  font-size: 14px;
}
.loginForm {
  margin-top: 30px;
}
</style>
